package com.shujia.flink.window

import org.apache.flink.api.common.eventtime.WatermarkStrategy
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.connector.kafka.source.KafkaSource
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo1TImeWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val source: KafkaSource[String] = KafkaSource
      .builder[String]
      //kafka broker集群列表
      .setBootstrapServers("master:9092")
      //执行消费的topic
      .setTopics("lines")
      //指定消费者组，一条数据在一个组内只被消费一次
      .setGroupId("shujia")
      .setStartingOffsets(OffsetsInitializer.earliest)
      .setValueOnlyDeserializer(new SimpleStringSchema())
      .build

    //使用kafka source
    val kafkaDS: DataStream[String] = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source")

    val wordsDS: DataStream[String] = kafkaDS.flatMap(_.split(","))

    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
     * 时间窗口
     * 滑动窗口窗口之间有交叉部分，滚动窗口创建之间没有交叉
     * 如果要使用事件时间数据中需要带一个时间字段，
     * TumblingProcessingTimeWindows: 滚动的处理时间窗口
     * TumblingEventTimeWindows：滚动的事件时间窗口
     * SlidingProcessingTimeWindows：滑动的处理时间窗口
     * SlidingEventTimeWindows：滑动的事件时间窗口
     *
     */

    //每集最近15秒单词的数量，每隔5秒统计一次
    val windowDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS
      .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))

    //窗口内求和
    val countDS: DataStream[(String, Int)] = windowDS.sum(1)

    countDS.print()

    env.execute()


  }

}
